<?php
namespace app\model;
use app\BaseModel;
use app\model\Member;
use think\model\concern\SoftDelete;

class MemberWithdraw extends BaseModel
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $hidden = ['update_time','delete_time','sort','status'];
    protected $append = ['audit_title','type_title'];


    public function searchMemberIdAttr($query,$value,$data){
        if(!empty($value)){
            $query->where('member_id','=',$value);
        }
    }
    public function searchTypeIdAttr($query,$value,$data){
        if(!empty($value)){
            $query->where('type_id','=',$value);
        }
    }
    public function searchAuditStatusAttr($query,$value,$data){
        if(!empty($value)){
            $query->where('audit_status','=',$value);
        }
    }


    protected function setMemberIdAttr($value,$data){ //当前字段的修改器
         $member = Member::where('id',$value)->find();
         $this->set('member_mobile',$member['mobile']);
         $this->set('member_name',$member['name']);
         return $value;
    }


    public function getTypeTitleAttr($value,$data)
    {
        $status = [1=>'微信',2=>'支付宝',3=>'银行卡'];
        return $status[$data['type_id']];
    }

    public function getAuditTitleAttr($value,$data)
    {
        $status = [1=>'待审核中',2=>'审核成功',3=>'审核失败'];
        return $status[$data['audit_status']];
    }

    public static function saveData($member_id, $type =2, $mark = '+', $brokerage,$withdraw_no,$withdraw_name)
    {
        $member_mobile = Member::where('id', $member_id)->value('mobile') ?? '';
        if ($mark == '-') {
            $member_brokerage = Member::where('id', $member_id)->value('brokerage');
            $mw_ret = self::create(['order_number'=> order_number(),'withdraw_no'=> $withdraw_no,'withdraw_name'=> $withdraw_name,'member_id' => $member_id, 'member_mobile' => $member_mobile, 'type_id' => $type, 'mark' => $mark, 'brokerage' => $brokerage, 'before' => $member_brokerage, 'after' => bcsub($member_brokerage, $brokerage, 2)]);
            Member::where('id', $member_id)->dec('brokerage', $brokerage)->update();
        }
        if ($mark == '+') {
            $member_brokerage = Member::where('id', $member_id)->value('brokerage');
            $mw_ret = self::create(['order_number'=> order_number(),'withdraw_no'=> $withdraw_no,'withdraw_name'=> $withdraw_name,'member_id' => $member_id, 'member_mobile' => $member_mobile, 'type_id' => $type, 'mark' => $mark, 'brokerage' => $brokerage, 'before' => $member_brokerage, 'after' => bcadd($member_brokerage, $brokerage, 2)]);
            Member::where('id', $member_id)->inc('brokerage', $brokerage)->update();
        }
        return true;
    }


}